<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        h2 {
            text-align: center;
        }
        .box {
            width: 80%;
            height: 500px;
            border: 4px solid #ccc;
            margin: 0 auto;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 200px;

        }
    </style>
</head>
<body>
    <h2>JS节流-定时器</h2>
    <div class="box"></div>
</body>
<script>
  
   let i=1;
   const box=document.querySelector('.box')
   box.textContent=i;

   box.addEventListener('mousemove',throttle(moveFn,3000),false)

   function moveFn(e) {
      i++;
      this.textContent=i;
   }


   function throttle(fn,waiting=1000) {
    //节流
      let timer=null

      return function() {
            let _this=this;
            let args=arguments

          if(!timer) {
            timer=setTimeout(function() {
                timer=null;
                fn.apply(_this,args)
            },waiting)
          }
       
      }
   }

</script>
</html>